package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllAdditiveaddresss", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress"),
		@NamedQuery(name = "findAdditiveaddressByName", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.name = ?1"),
		@NamedQuery(name = "findAdditiveaddressByNameContaining", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findAdditiveaddressByTime", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.time = ?1"),
		@NamedQuery(name = "findAdditiveaddressByPrice", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.price = ?1"),
		@NamedQuery(name = "findAdditiveaddressByDescription", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.description = ?1"),
		@NamedQuery(name = "findAdditiveaddressByDescriptionContaining", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllAdditiveaddresssByAdditive", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.additive.id = ?1"),
		@NamedQuery(name = "findAllAdditiveaddresssByAddress", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.address.id = ?1"),
		@NamedQuery(name = "findAllAdditiveaddresssByUnit", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.unit.id = ?1"),
		@NamedQuery(name = "findAdditiveaddressById", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.id = ?1"),
		@NamedQuery(name = "findAdditiveaddressByPrimaryKey", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.id = ?1 "),
		@NamedQuery(name = "deleteAdditiveaddresssByIds", query = "delete Additiveaddress where id in (?1)"),
		@NamedQuery(name = "findAdditiveaddresssByIds", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where myAdditiveaddress.id in (?1)"),
		@NamedQuery(name = "findAdditiveaddressByPhyscialPrimaryKey", query = "select myAdditiveaddress from Additiveaddress myAdditiveaddress where  myAdditiveaddress.additive.id = ?1 and myAdditiveaddress.address.id = ?2 and myAdditiveaddress.time = ?3"),
		@NamedQuery(name = "countAdditiveaddresssByName", query = "select count(myAdditiveaddress)  from Additiveaddress myAdditiveaddress where myAdditiveaddress.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "additiveaddress")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Additiveaddress")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Additiveaddress implements Serializable, Comparable<Additiveaddress> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "time")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar time;

	@Column(name = "price", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal price;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "additive", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Additive additive;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "address", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Address address;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "unit", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Unit unit;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setTime(Calendar time) {
		this.time = time;
	}

	public Calendar getTime() {
		return this.time;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public BigDecimal getPrice() {
		return this.price;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setAdditive(Additive additive) {
		this.additive = additive;
	}

	public Additive getAdditive() {
		return additive;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public Address getAddress() {
		return address;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public Unit getUnit() {
		return unit;
	}

	public void copy(Additiveaddress that) {

		setId(that.getId());

		setName(that.getName());
		setTime(that.getTime());
		setPrice(that.getPrice());
		setDescription(that.getDescription());

		setAdditive(that.getAdditive());

		setAddress(that.getAddress());

		setUnit(that.getUnit());

	}

	public Additiveaddress() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("time=[").append(time).append("] ");
		buffer.append("price=[").append(price).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Additiveaddress))
			return false;
		Additiveaddress comparedAdditiveaddress = (Additiveaddress) obj;
		if (!CheckPropertyEqual(this.additive.id, comparedAdditiveaddress.additive.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.address.id, comparedAdditiveaddress.address.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.time, comparedAdditiveaddress.time)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Additiveaddress o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
